﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace ExamServer.Forms
{
    public partial class DialogRelease : Form
    {
        public DialogRelease()
        {
            InitializeComponent();
        }

        private void ReleaseDlg_Load(object sender, EventArgs e)
        {
            textBox1.Text = (Properties.Settings.Default.ClientVersion + 1).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
            {
                foreach (string fn in openFileDialog1.FileNames)
                {
                    listBox1.Items.Add(fn);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] str = new string[listBox1.SelectedItems.Count];
            for(int i=0; i < listBox1.SelectedItems.Count; i++)
            {
                str[i] = (string)(listBox1.SelectedItems[i]);
            }
            for (int i = 0; i < str.Length; i++)
            {
                listBox1.Items.Remove(str[i]);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string[] file = new string[listBox1.Items.Count];
                string[] fileName = new string[listBox1.Items.Count];
                DBConnection db = new DBConnection();
                int newVersion = Properties.Settings.Default.ClientVersion + 1;
                if (!Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "\\ClientReleaseFiles"))
                {
                    Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + "\\ClientReleaseFiles");
                }
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    file[i] = (string)(listBox1.Items[i]);
                    fileName[i] = Path.GetFileName(file[i]);
                    File.Copy(file[i], System.AppDomain.CurrentDomain.BaseDirectory + "\\ClientReleaseFiles\\" + fileName[i], true);
                    db.ExecuteSql("insert into tb_ClientUpdate(version, fileName) values(" + newVersion + ",'" + fileName[i] + "')");
                }
                Properties.Settings.Default.ClientVersion = newVersion;
                Properties.Settings.Default.Save();
                MessageBox.Show("发布成功！");
                this.Close();
            }
            catch(Exception)
            {
                MessageBox.Show("发布失败！");
            }
        }
    }
}
